package com.mySampleApplication.client.components;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mySampleApplication.client.CodeRuleAtter.CodeRuleAtterDto;
import com.mySampleApplication.client.CodeRuleAtter.CodeRuleAtterService;
import com.mySampleApplication.client.codeRule.CodeRuleDto;
import com.mySampleApplication.client.codeRule.CodeRuleService;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import java.util.*;

public class DataElementCategoryDataSource {
    public static DataSource getDataSource(){
// 创建一个客户端数据源，用于存储静态数据
        DataSource dataSource = new DataSource();
        dataSource.setClientOnly(true); // 设置为客户端数据源

        // 定义数据源字段
        DataSourceIntegerField idFieldDS = new DataSourceIntegerField("id");
        idFieldDS.setPrimaryKey(true);
//        DataSourceTextField db_model_idFieldDS = new DataSourceTextField("db_model_id");
//        DataSourceTextField mcFieldDS = new DataSourceTextField("mc");
//        DataSourceTextField msFieldDS = new DataSourceTextField("ms");
//        DataSourceTextField fgfFieldDS = new DataSourceTextField("fgf");
//        DataSourceTextField qzFieldDS = new DataSourceTextField("qz");
//        DataSourceTextField hzFieldDS = new DataSourceTextField("hz");
//        DataSourceTextField statusFieldDS = new DataSourceTextField("status");

        DataSourceIntegerField fdmcField = new DataSourceIntegerField("fdmc", "分段名称");
        DataSourceIntegerField fdsxField = new DataSourceIntegerField("fdsx", "分段顺序");
        //TextItem fdlxField = new TextItem("fdlx", "分段类型");
        DataSourceIntegerField fdlxField = new DataSourceIntegerField("fdlx", "分段类型");
        DataSourceIntegerField lshcdField = new DataSourceIntegerField("lshcd", "流水号长度");
        DataSourceIntegerField lshbcField = new DataSourceIntegerField("lshbc", "流水号步长");
        DataSourceIntegerField lshgsField = new DataSourceIntegerField("lshgs", "流水号前缀");
        DataSourceIntegerField gdzField= new DataSourceIntegerField("gdz", "固定值");
        DataSourceIntegerField zdsxmField= new DataSourceIntegerField("zdsxm", "字段属性名");

        // 将字段添加到数据源中
        dataSource.setFields(idFieldDS, fdmcField,fdsxField,fdlxField,lshcdField, lshbcField,lshgsField,gdzField,zdsxmField);


        Map<String, String> mapPage = new HashMap<>();
        mapPage.put("pageSize","1");
        mapPage.put("pageNumber","10");

        CodeRuleAtterService.App.getInstance().getSelect(null,mapPage, new AsyncCallback<CodeRuleAtterDto>() {
            public void onSuccess(CodeRuleAtterDto result) {
                List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                List<ListGridRecord> recordList = new ArrayList<>();
                for (Map<String, String> data : keyValueMap) {
                    ListGridRecord record = new ListGridRecord();
                    data.forEach((k,v)->{
                        record.setAttribute(k,v);
                    });
                    // 设置其他需要的属性
                    recordList.add(record);
                }
                dataSource.setTestData(recordList.toArray(new ListGridRecord[0]));
                //SC.say( "获取数据成功！");
            }
            public void onFailure(Throwable caught) {
                SC.warn("获取数据失败！");
            }
        });
        // 将静态数据添加到数据源
        return dataSource;
    }

}
